import {observable, action,} from 'mobx';
import * as HistoryApi from '../../api/news';
import { SearchPage  } from 'weaver-mobile-page'; 
import ListStore from './ListStore';
import * as API from '../../api/netdisk';

export default class NetdiskStore {
  @observable listStore = new ListStore({
    netdiskStore:this
  });
  @observable selectedTab = 'disk';
  @observable categoryInfo = {
    id: '',
    parentId: '',
    name:''
  };
  @observable searchPage = new SearchPage.Store({ route: '/netdisk/history' }); 
  // @observable folderIdIndex = ""; //当前所在的目录的id
  searchType = 7;  //云盘文件搜索类型
  searchKey = {};  //搜索条件
  @observable multiOper = false;
  @observable isOpenFile = false; //判断当前附件是否是打开状态
  @observable searchFolderid = "";  //当前搜索时所在的目录
  @observable diskLimitType = "";

  @action
  setSelectedTab = (tab,params) => {
    this.selectedTab = tab;
    if (this.selectedTab === 'disk') {
      this.listStore.getMyList(params);
    } else if (this.selectedTab === 'myShare') {
      this.listStore.getMyShareList(params);
    } else if(this.selectedTab === 'shareMy') {
      this.listStore.getWorkmateShareList(params);
    }
  }
  getwhietlist = ()=>{
    API.getwhietlist().then((res)=>{
      this.diskLimitType = res.diskType;
    })
  }
  //添加搜索历史
  addHistory = (keyword) =>{
    HistoryApi.addHistory({searchtext:keyword,type:this.searchType}) 
  }
  //获取搜索历史
  getHistory = () => {
    const params = {type:this.searchType}; 
    HistoryApi.getHistoryList(params).then((result) => {
        this.historyList = [];
        result.historyData.map((data,i)=>{
          return this.historyList.push({
            key : i,
            name : data,
          });
        });
        this.searchPage.list.setState({data : this.historyList});
    }); 
  }
  //初始化搜索历史
  setSearchHistory = () => {
    const template = {
      default: [
        {
          key: 'col1',
          configs: [
            {
              key: 'col1_row1',
              configs: [
                { key: 'name' },
              ],
            },
          ],
        },
      ],
    };
    this.searchPage.setState({ template });
    this.getHistory();
  }
    //搜索
  doSearchByKeyword = (keyword,folderid) => {
    let params = {keyword : keyword,bySearch : keyword ? 1 : 0}
    this.searchKey = params ? params : {}
    params = {...params,folderid}
    this.setSelectedTab(this.selectedTab,params)
  }
  //清空搜索历史
  clearSearchHistory = () => {
    const params = {type:this.searchType};
    HistoryApi.clearSearchHistory(params).then((result) => {
        this.historyList = [];
        this.searchPage.list.setState({data : this.historyList});  
    }); 
  }
}